package com.hellogood.yaohuo.client.setting;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.entity.Org;
import com.hellogood.yaohuo.shared.services.OrgService;
import com.hellogood.yaohuo.shared.services.QyOrgService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DragDataAction;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.TransferImgButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.layout.VStack;

public class QuyuSettingWindow extends Window {
	private String quyuCode = null;
	private String quyuName = null;

	private ListGrid unClassfiedOrgListGrid = null;
	private ListGrid orgUnderQuyuListGrid = null;
	private Button btn = null;

	public QuyuSettingWindow(String quyuCode, String quyuName) {
		this.quyuCode = quyuCode;
		this.quyuName = quyuName;
		initData();
		initView();
	}

	private void initData() {
		OrgService.Util.getInstance().getUnQuyuedOrgList(new UnQuyuedOrgListCallback());
		OrgService.Util.getInstance().getOrgListByQuyuCode(quyuCode, new OrgListByQuyuCallback());
	}

	private void initView() {
		ListGridField code1Field = new ListGridField("code");
		code1Field.setHidden(true);
		ListGridField name1Field = new ListGridField("name", "组织");
		unClassfiedOrgListGrid = new ListGrid();
		unClassfiedOrgListGrid.setWidth(300);
		unClassfiedOrgListGrid.setHeight(300);
		unClassfiedOrgListGrid.setCanDragRecordsOut(true);
		unClassfiedOrgListGrid.setDragDataAction(DragDataAction.MOVE);
		unClassfiedOrgListGrid.setFields(code1Field, name1Field);

		ListGridField code2Field = new ListGridField("code");
		code2Field.setHidden(true);
		ListGridField name2Field = new ListGridField("name", "组织");
		orgUnderQuyuListGrid = new ListGrid();
		orgUnderQuyuListGrid.setWidth(300);
		orgUnderQuyuListGrid.setHeight(300);
		orgUnderQuyuListGrid.setCanDragRecordsOut(true);
		orgUnderQuyuListGrid.setDragDataAction(DragDataAction.MOVE);
		orgUnderQuyuListGrid.setCanAcceptDroppedRecords(true);
		orgUnderQuyuListGrid.setCanRemoveRecords(true);
		orgUnderQuyuListGrid.setPreventDuplicates(true);
		orgUnderQuyuListGrid.setFields(code2Field, name2Field);

		HStack hStack = new HStack(10);
		hStack.setHeight(350);

		Label label = new Label();
		label.setHeight(50);
		label.setContents("未分配组织");
		VStack vStack = new VStack();
		vStack.addMember(label);
		vStack.addMember(unClassfiedOrgListGrid);
		hStack.addMember(vStack);

		TransferImgButton arrowImg = new TransferImgButton(TransferImgButton.RIGHT);
		arrowImg.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				unClassfiedOrgListGrid.transferSelectedData(orgUnderQuyuListGrid);
			}
		});
		hStack.addMember(arrowImg);

		Label label2 = new Label();
		label2.setHeight(50);
		label2.setContents(quyuName);
		VStack vStack2 = new VStack();
		vStack2.addMember(label2);
		vStack2.addMember(orgUnderQuyuListGrid);

		hStack.addMember(vStack2);

		hStack.setSize("100%", "100%");

		HLayout hLayout = new HLayout();
		btn = new Button("保存");
		btn.setAlign(Alignment.CENTER);
		btn.addClickHandler(new SaveBtnClickHandler());
		hLayout.setAlign(Alignment.CENTER);
		hLayout.addMember(btn);
		hLayout.setVertical(true);
		hLayout.setPadding(20);

		VLayout layout = new VLayout();
		layout.setTop(10);
		layout.setLeft(60);
		layout.setMembers(hStack, hLayout);
		addChild(layout);
	}

	private class UnQuyuedOrgListCallback implements AsyncCallback<List<Org>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Org> result) {
			if (result != null) {
				Record[] records = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					r.setAttribute("code", result.get(i).getOrgCode());
					r.setAttribute("name", result.get(i).getOrgName());
					records[i] = r;
				}
				unClassfiedOrgListGrid.setData(records);
			}
		}
	}

	private class OrgListByQuyuCallback implements AsyncCallback<List<Org>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Org> result) {
			if (result != null) {
				Record[] records = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Record r = new Record();
					r.setAttribute("code", result.get(i).getOrgCode());
					r.setAttribute("name", result.get(i).getOrgName());
					records[i] = r;
				}
				orgUnderQuyuListGrid.setData(records);
			}
		}

	}

	private class SaveBtnClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			ListGridRecord[] records = orgUnderQuyuListGrid.getRecords();
			if (records != null) {
				List<String> orgCodeList = new ArrayList<String>();
				for (ListGridRecord r : records) {
					orgCodeList.add(r.getAttribute("code"));
				}
				QyOrgService.Util.getInstance().updateOrgListForQuyu(orgCodeList, quyuCode, new SaveOrgListQuyuCallback());
			}
		}
	}

	private class SaveOrgListQuyuCallback implements AsyncCallback<String> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(String result) {
			if (result == null) {
				SC.say("保存错误");
			} else if (result.equals("")) {
				SC.say("保存成功");
				btn.setDisabled(true);
			} else {
				SC.say(result);
			}
		}
	}
}
